<template>
  <view class="padding-lg bg-gray" style="min-height:100vh">
    <view class="padding">
      <evan-form :hide-required-asterisk="true" :label-style="labelStyles" 
        ref="form" :model="formInfo" class="no-bottom-border">

        <evan-form-item label="" prop="idno" label-position="left">
          <view class="i-input">
            <input type="text" v-model="formInfo.userName" placeholder="请输入职工号" />
          </view>
        </evan-form-item>


        <view class="margin-top-xl">
          <button :disabled="submiting" @click="onSubmit" class="cu-btn lg btn-fluid bg-primary">登录</button>
        </view>
      </evan-form>
    </view>
  </view>
</template>

<script>
import { mapActions } from 'vuex'
import CheckAuthMixin from '@/mixins/CheckAuthMixin'

const FormInfo = {
  userName: ''
}

export default {
  mixins: [
    CheckAuthMixin,
  ],
  data() {
    return {
      needCheckAuth: false,
      labelStyles: {
        minWidth: '80px',
        textAlign: 'justify',
        paddingRight: '30upx',
        fontSize: '30upx',
        color: 'inherit',
      },
      submiting: false,
      formInfo: Object.assign({}, FormInfo),
      formRules: {
        userName: [
          { required: true, message: '请输入职工号' },
        ]
      },
    }
  },
  watch: {
    submiting(val) {
      if (val) {
        uni.showLoading()
      } else {
        uni.hideLoading()
      }
    },
  },
  onLoad(opts) {
    this.$utils.pLog('== onLoad: ', this.formInfo)
  },
  mounted() {
    this.$refs.form.setRules(this.formRules)
  },
  methods: {
    ...mapActions('session', ['getUserInfo']),
    onSubmit() {
      this.$refs.form.validate((res) => {
        if (res) {
          this.$utils.pLog('== submiting: ', this.formInfo)

          const { userName } = this.formInfo
          this.$http({
            url: `/api/InHospital/Authenticate?oper_id=${userName}`,
            method: 'POST',
            success: (res) => {
              if (res.success) {
                this.getUserInfo({
                  userName,
                  token: res.data
                })
                  .then(() => {

                    this.gotoScan()
                  })
              } else {
                uni.showToast({
                  title: res.msg,
                  icon: 'none'
                })
              }
            }
          })
          
        }
      })
    },
    gotoScan() {
      uni.navigateTo({
        url: `/pages/hesuanyuyue/scan/index`
      })
    },
  },
}
</script>

<style scoped>
.bg-cus-blue {
  background: #3182F8;
}
.no-bottom-border /deep/ .evan-form-item-container {
  border-bottom: none;
}
</style>